#include <stdlib.h>
#include <stdio.h>
#include <string.h>

extern void search(char**,unsigned[]);

char *exe_name;

void usage(){
	printf("%s DNA1file DNA2file\n",exe_name);
	exit(EXIT_SUCCESS);
}

char* process_file(FILE* f,unsigned *rd){//Allocates memory
	char* out=0;
	unsigned len=0;
	unsigned red=0;
	
	fseek(f,0,SEEK_END);
	len=ftell(f);
	fseek(f,0,SEEK_SET);

	out=(char*)malloc(len+81);

	
	while(!feof(f) ){
		if(len<red){
			printf("wrong memory info\n");
			free(out);
			return 0;
		}
		if(fgets(out+red,81,f)==0 ){
			if(feof(f))break;
			perror(0);
			return 0;
		}
		red+=strlen(out+red);
		if(out[red-1]=='\n')
			out[--red]=0;
	}

	*rd=red;
	return out;
}

int main(int argc, char* argv[]){
	FILE* D;
	int i;
	char* DNA[2];
	unsigned DNAlen[2];

	DNA[0]=DNA[1]=0;
	DNAlen[0]=DNAlen[1]=0;

	exe_name=*argv;
	if(argc<3)usage();
	
	for(i=0;i<2;i++){
		D=fopen(argv[1+i],"r");
		if(D==(FILE*)0){
			perror(argv[1+i]);
			if(DNA[0]!=0)free(DNA[0]);
			exit(EXIT_FAILURE);
		}
		DNA[i]=process_file(D,DNAlen+i);
		fclose(D);
		if(DNA[i]==0){
			if(DNA[0]!=0)free(DNA[0]);
			return 1;
		}
	}

	search(DNA,DNAlen);
	
	free(DNA[0]);free(DNA[1]);
	return 0;
}
